package database;

import entity.TeacherClassDetails;
import manager.TeacherClassDetailsManager;
import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Noi
 */
public class TeacherClassDetailsManagerDB {

    private CallableStatement cstmt;
    private java.sql.Connection conn;
    private ResultSet rs;

    public TeacherClassDetailsManagerDB(java.sql.Connection conn) {
        this.conn = conn;
    }

    public void loadData(TeacherClassDetailsManager teacherClassDetailsManager) {
        try {
            cstmt = conn.prepareCall("{call sp_GetTeacherClassDetails}");
            rs = cstmt.executeQuery();
            while (rs.next()) {
                int teacherClassDetailsID = rs.getInt("TeacherClassDetailsID");
                String classID = rs.getString("ClassID");
                int teacherID = rs.getInt("TeacherID");
                boolean status = rs.getBoolean("Status");
                TeacherClassDetails subjectDetails = new TeacherClassDetails(teacherClassDetailsID,classID, teacherID, status);
                teacherClassDetailsManager.add(subjectDetails);
            }
            rs.close();
            cstmt.close();
        } catch (SQLException ex) {
            Logger.getLogger(TeacherClassDetailsManagerDB.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void deleteTeacherClassDetails(TeacherClassDetails obj) {
        try {
            cstmt = conn.prepareCall("{call sp_DeleteTeacherClassDetails(?)}");
            cstmt.setInt(1, obj.getTeacherClassDetailsID());
            cstmt.executeUpdate();
            obj.setTeacherClassDetailsID(obj.getTeacherClassDetailsID());
            cstmt.close();
        } catch (SQLException ex) {
            Logger.getLogger(TeacherClassDetailsManagerDB.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void insertTeacherClassDetails(TeacherClassDetails obj) {
        try {
            cstmt = conn.prepareCall("{call sp_InsertTeacherClassDetails(?,?,?)}");
            cstmt.registerOutParameter(1, Types.INTEGER);
            cstmt.setString(2, obj.getClassID());
            cstmt.setInt(3, obj.getTeacherID());
            cstmt.executeUpdate();
            obj.setTeacherClassDetailsID(cstmt.getInt("TeacherClassDetailsID"));
            cstmt.close();
        } catch (SQLException ex) {
            Logger.getLogger(TeacherClassDetailsManagerDB.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void updateTeacherClassDetails(TeacherClassDetails obj) {
        try {
            cstmt = conn.prepareCall("{call sp_UpdateTeacherClassDetails(?,?,?,?)}");
            cstmt.setInt(1, obj.getTeacherClassDetailsID());
            cstmt.setString(2, obj.getClassID());
            cstmt.setInt(3, obj.getTeacherID());
            cstmt.setBoolean(4, obj.isStatus());
            cstmt.executeUpdate();
            cstmt.close();
        } catch (SQLException ex) {
            Logger.getLogger(TeacherClassDetailsManagerDB.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
